package DCRS::Stats;
#----------------------------------------------------------------------------
# Name:         Stats.pm
# Purpose:      Display peridocally collected stats from DCRS .DAT files
# 
# Notes:        This module is part of the DCRS Dispatch Web Interface 
#               It relies on data provided by:
#               <PROJECT ROOT>/bin/getcallstats.pl 
#
# Author:       Kingsley Wright
#
# Created:      2007-2008
# Copyright:    (c) 2008 by DCRS Solutions, Inc.
# Licence:      TBD - All rights reversed, DCRS Solutions, Inc.
#----------------------------------------------------------------------------
use base 'CGI::Application';
use Tie::File::AsHash; # Using pregenerated files for speed where 
                       # timeliness of data is not critical 
                       
# This is where the .dat files are kept.
my $DBPATH = '/media/officedb/PUBLIC/OFFICE4/';

# Globals for datetime manipulation.
my $localtime = localtime();
   
sub setup {
    # Define run modes here
    # url mappings in [PROJECT_ROOT]/bin/dispatch.cgi
    my $self = shift;
    $self->start_mode('start');
    $self->run_modes(
        all  => 'show_start_page',
    );
}

sub show_start_page {
    my $self = shift;
    my $title = "DCRS Custom Reports &amp; Statistics";
    my $motd = "This is where a message of the day could be placed";
    
    my $template = $self->load_tmpl(
        'stats/stats.tmpl',
        global_vars       => 1,
        loop_context_vars => 1,
        die_on_bad_params => 0
    );
    
    tie my %stats, 'Tie::File::AsHash', $DBPATH .'call_stats.dbh',
     split => ':'
        or die "Problem tying %stats: $!";
        
    $template->param(
        title       => $title,
        localtime   => $localtime,
        motd        => $motd,
        nonotes     => $stats{nonotes},
        nonotes24   => $stats{nonotes24},
        nonotes48   => $stats{nonotes48},
        opentotal   => $stats{opentotal},
        closedtotal => $stats{closedtotal},
    );
    
    untie %stats;
    return $template->output();    
}
1;
